//
//  NSString+CLRegex.m
//  Rippton_iphone
//
//  Created by yeyongping on 2017/11/6.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "NSString+CLRegex.h"

@implementation NSString (CLRegex)
/**
 判断是不是正则表达式
 
 @return 返回 YES : 符合正则表达式  返回 NO : 不符合正则表达式
 */
- (BOOL)cl_compareWithRegexString:(NSString *)regexString{
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexString];
    return  [predicate evaluateWithObject:self];
}

//正则表达式校验邮箱
- (BOOL)cl_isEmailAddress{
    NSString *emailString = @"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailString];
    return  [predicate evaluateWithObject:self];
}
- (BOOL)cl_isPassword{
    NSString *passwordString = @"^[0-9a-zA-Z]+$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", passwordString];
    return [predicate evaluateWithObject:self];
}

- (BOOL)cl_isWebUrl{
    NSString *webString = @"((http|ftp|https)://)(([a-zA-Z0-9\\._-]+\\.[a-zA-Z]{2,6})|([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\\&%_\\./-~-]*)?";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",webString];
    return [predicate evaluateWithObject:self];
}

- (BOOL)cl_isPhone{
    NSString *phoneRegex = @"^1(3[0-9]|4[56789]|5[0-9]|6[6]|7[0-9]|8[0-9]|9[189])\\d{8}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [predicate evaluateWithObject:self];
}

- (BOOL)cl_html5{
    
    if (self.length == 0) {
        return NO;
    }
    
    NSRegularExpression *regularExpretion = [NSRegularExpression regularExpressionWithPattern:@"<[^>]*>|\n"
                                                                                        options:0
                                                                                          error:nil];
    NSArray* match = [regularExpretion matchesInString:self options:0 range:NSMakeRange(0, [self length])];
    
    return match.count > 0;
}


@end
